package com.leetcode.problems050;

import java.util.*;

/**
 * https://leetcode-cn.com/problems/powx-n/
 * @Date 2021/12/09 14:34
 * @Author ghery
 * @Version V1.0
 */
public class Solution {
    public double myPow(double x, int n) {
        long N = n;
        return N > 0 ? pow(x,N) : 1.0 / (pow(x,-N));
    }


    public double pow(double x, long n) {
        if(n == 0){
            return 1.0;
        }
        double y = pow(x,n / 2);
        return n % 2 == 0 ? y * y : y * y * x;
    }


    public static void main(String[] args) {
        System.out.println(new Solution().myPow(2.0,4));
    }
}
